import {user, register, verify, updateUserInfo} from "@/api/user";
import baseUrl from "@/api/baseUrl";
// import crypto from "@/utils/crypto";

export default {
    state: {
        id: "",
        user: "", // 登录的用户邮箱
        username: "", // 用户昵称
        avatar: "",
        // token: "",
        balance: 0,
        role: 1, // 用户角色
        token: "SUSteam-eyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiIxMiIsInN1YiI6InpoYW5nIiwiaWF0IjoxNjA4NzkyNDgwLCJpc3MiOiJzYW5zIiwiZXhwIjoxNjA4ODc4ODgwLCJhdXRob3JpdGllcyI6Ilt7XCJhdXRob3JpdHlcIjpcIlJPTEVfVVNFUlwifV0ifQ.-SwwcoZnMC0b6pEFNv3X3Ez5mCLkIc7cy8DCjJON9h1tsx_lykfHaG7lNhiOY2CNkpFlrbKmbvKA8OmAxsBeoQ",
        toLogin: false, // 用于控制是否显示登录组件
        toEdit: false, // 用于控制是否显示编辑用户组件
        default_avatar: baseUrl + "avatar/default_avatar.png", // 头像图片的地址
    },
    getters: {
        getUser(state) {
            return state.user
        },
        getAvatar(state) {
            return state.avatar
        },
        getDefaultAvatar(state) {
            return state.default_avatar
        },
        getToLogin(state) {
            return state.toLogin
        },
        getToken(state) {
            return state.token
        },
        getBalance(state) {
            return state.balance
        },
        getToEdit(state) {
            return state.toEdit
        },
        getRole(state) {
            return state.role
        },
        getUsername(state) {
            return state.username
        },
        getId(state) {
            return state.id;
        },
        getUserInfo(state) {
            return {
                id: state.id,
                user: state.user,
                username: state.username,
                avatar: state.avatar,
                balance: state.balance
            };
        }
    },
    mutations: {
        setUser(state, data) {
            state.user = data;
        },
        setUsername(state, data) {
            state.username = data;
        },
        setAvatar(state, data) {
            state.avatar = data;
        },
        setToLogin(state, data) {
            state.toLogin = data;
        },
        setToken(state, data) {
            state.token = data;
        },
        setBalance(state, data) {
            state.balance = data;
        },
        setToEdit(state, data) {
            state.toEdit = data
        },
        setRole(state, data) {
            state.role = data
        },
        setId(state, data) {
            state.id = data
        },
        setUserInfo(state, data) {
            state.id = data.accountId;
            state.user = data.email;
            state.username = data.username;
            state.avatar = baseUrl + data.avatar.slice(1);
        }
    },
    actions: {
        Login({commit}, userInfo) { //定义 Login 方法，在组件中使用 this.$store.dispatch("Login") 调用
            const email = userInfo.email.trim()
            return new Promise((resolve, reject) => { //封装一个 Promise
                user(email, userInfo.passwd).then(response => { //使用 user 接口进行网络请求
                    commit('setUser') //提交一个 mutation，通知状态改变
                    resolve(response) //将结果封装进 Promise
                }).catch(error => {
                    reject(error)
                })
            })
        },
        Register({commit}, userInfo) { //定义 Login 方法，在组件中使用 this.$store.dispatch("Login") 调用
            const username = userInfo.email.trim()
            return new Promise((resolve, reject) => { //封装一个 Promise
                register(username, userInfo.passwd, userInfo.verify).then(response => { //使用 user 接口进行网络请求
                    commit('setToken') //提交一个 mutation，通知状态改变
                    resolve(response) //将结果封装进 Promise
                }).catch(error => {
                    reject(error)
                })
            })
        },
        Verify({commit}, userInfo) {
            const email = userInfo.email.trim()
            return new Promise((resolve, reject) => { //封装一个 Promise
                verify(email).then(response => { //使用 user 接口进行网络请求
                    commit('setUser') //提交一个 mutation，通知状态改变
                    resolve(response) //将结果封装进 Promise
                }).catch(error => {
                    reject(error)
                })
            })
        },
        changeUsername({commit}, token, userInfo) {
            return new Promise((resolve, reject) => {
                updateUserInfo(token, userInfo).then(response => {
                    commit('setUsername')
                    resolve(response)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        setUser({commit}, data) {
            commit('setUser', data);
        },
        setUsername({commit}, data) {
            commit('setUsername', data);
        },
        setAvatar({commit}, data) {
            commit('setAvatar', data)
        },
        setToLogin({commit}, data) {
            commit('setToLogin', data);
        },
        setToken({commit}, data) {
            commit('setToken', data);
        },
        setBalance({commit}, data) {
            commit('setBalance', data);
        },
        setToEdit({commit}, data) {
            commit('setToEdit', data)
        },
        setRole({commit}, data) {
            commit('setRole', data)
        },
        setId({commit}, data) {
            commit('setId', data)
        },
        setUserInfo({commit}, data) {
            commit('setUserInfo', data)
        }
    }
}